let controller = require("./controller");
let mydebug = require('./mydebug');

let schedule = require('node-schedule'); 

let current_pwm = [100, 100, 100];

let turnon_time = '00 08 * * *';          // Everyday  08:00 turn on
let turnoff_time = '00 20 * * *';        // Everyday  20:00 turn off

let timeslots = [
    {slot: '00 00 * * *', fun: set0},
    {slot: '00 06 * * *', fun: set5},
    {slot: '00 07 * * *', fun: set10},
    {slot: '00 09 * * *', fun: set50},
    {slot: '00 11 * * *', fun: set100},
    {slot: '00 12 * * *', fun: set150},
    {slot: '00 13 * * *', fun: set200},
    {slot: '00 14 * * *', fun: set250},
    {slot: '00 16 * * *', fun: set200},
    {slot: '00 17 * * *', fun: set150},
    {slot: '00 18 * * *', fun: set100},
    {slot: '00 19 * * *', fun: set50},
    {slot: '00 20 * * *', fun: set10},
    {slot: '00 21 * * *', fun: set5},
    {slot: '00 22 * * *', fun: set0}
]

timeslots.forEach(function(t) {
    schedule.scheduleJob(t.slot, t.fun);
})


function set0() {
    for (i = 0; i < 3; i++) {
        current_pwm[i] = 0;
        controller.setLEDpwm(i, current_pwm[i]);
    }
}

function set5() {
    for (i = 0; i < 3; i++) {
        current_pwm[i] = 5;
        controller.setLEDpwm(i, current_pwm[i]);
    }
}

function set10() {
    for (i = 0; i < 3; i++) {
        current_pwm[i] = 10;
        controller.setLEDpwm(i, current_pwm[i]);
    }
}

function set50() {
    for (i = 0; i < 3; i++) {
        current_pwm[i] = 50;
        controller.setLEDpwm(i, current_pwm[i]);
    }
}

function set100() {
    for (i = 0; i < 3; i++) {
        current_pwm[i] = 100;
        controller.setLEDpwm(i, current_pwm[i]);
    }
}

function set150() {
    for (i = 0; i < 3; i++) {
        current_pwm[i] = 150;
        controller.setLEDpwm(i, current_pwm[i]);
    }
}

function set200() {
    for (i = 0; i < 3; i++) {
        current_pwm[i] = 200;
        controller.setLEDpwm(i, current_pwm[i]);
    }
}

function set250() {
    for (i = 0; i < 3; i++) {
        current_pwm[i] = 250;
        controller.setLEDpwm(i, current_pwm[i]);
    }
}

function set300() {
    for (i = 0; i < 3; i++) {
        current_pwm[i] = 300;
        controller.setLEDpwm(i, current_pwm[i]);
    }
}

function set350() {
    for (i = 0; i < 3; i++) {
        current_pwm[i] = 350;
        controller.setLEDpwm(i, current_pwm[i]);
    }
}

function set400() {
    for (i = 0; i < 3; i++) {
        current_pwm[i] = 400;
        controller.setLEDpwm(i, current_pwm[i]);
    }
}

controller.controllerEvent.on('connect', function() {
    // restore
    for (i = 0; i < 3; i++) {
        controller.setLEDpwm(i, current_pwm[i]);
    }
})

controller.controllerEvent.on('disconnect', function() {

})
